﻿namespace Sidvall.IdentityManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class Plugin : global::Sidvall.IdentityManager.UI.Entities.Core.BusinessBase, global::Sidvall.IdentityManager.Data.IPlugin
    {
        #region Business Members

        #region Context

        private string _Context = string.Empty;
        partial void OnBeforeContextPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeContextPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterContextPropertyChanged();
        public string Context
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Context);
                OnBeforeContextPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Context);
                OnBeforeContextPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Context", eventData.Value, true, true, true);
                    OnAfterContextPropertyChanged();
                }
            }
        }

        #endregion
        #region EditIsEnabled

        private bool _EditIsEnabled = false;
        partial void OnBeforeEditIsEnabledPropertyGet(Sidvall.Business.PropertyEventData<bool> eventData);
        partial void OnBeforeEditIsEnabledPropertyChanged(Sidvall.Business.PropertyChangeEventData<bool> eventData);
        partial void OnAfterEditIsEnabledPropertyChanged();
        public bool EditIsEnabled
        {
            get
            {
                Sidvall.Business.PropertyEventData<bool> eventData = new Sidvall.Business.PropertyEventData<bool>(_EditIsEnabled);
                OnBeforeEditIsEnabledPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<bool> eventData = new Sidvall.Business.PropertyChangeEventData<bool>(value, _EditIsEnabled);
                OnBeforeEditIsEnabledPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("EditIsEnabled", eventData.Value, true, true, true);
                    OnAfterEditIsEnabledPropertyChanged();
                }
            }
        }

        #endregion
        #region FilterIsEnabled

        private bool _FilterIsEnabled = false;
        partial void OnBeforeFilterIsEnabledPropertyGet(Sidvall.Business.PropertyEventData<bool> eventData);
        partial void OnBeforeFilterIsEnabledPropertyChanged(Sidvall.Business.PropertyChangeEventData<bool> eventData);
        partial void OnAfterFilterIsEnabledPropertyChanged();
        public bool FilterIsEnabled
        {
            get
            {
                Sidvall.Business.PropertyEventData<bool> eventData = new Sidvall.Business.PropertyEventData<bool>(_FilterIsEnabled);
                OnBeforeFilterIsEnabledPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<bool> eventData = new Sidvall.Business.PropertyChangeEventData<bool>(value, _FilterIsEnabled);
                OnBeforeFilterIsEnabledPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("FilterIsEnabled", eventData.Value, true, true, true);
                    OnAfterFilterIsEnabledPropertyChanged();
                }
            }
        }

        #endregion
        #region PluginId

        private System.Nullable<int> _PluginId = null;
        partial void OnBeforePluginIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData);
        partial void OnBeforePluginIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterPluginIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<int> PluginId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<int>>(_PluginId);
                OnBeforePluginIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _PluginId);
                OnBeforePluginIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("PluginId", eventData.Value, true, true, true);
                    OnAfterPluginIdPropertyChanged();
                }
            }
        }

        #endregion
        #region ServerItemId

        private int _ServerItemId = 0;
        partial void OnBeforeServerItemIdPropertyGet(Sidvall.Business.PropertyEventData<int> eventData);
        partial void OnBeforeServerItemIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<int> eventData);
        partial void OnAfterServerItemIdPropertyChanged();
        public int ServerItemId
        {
            get
            {
                Sidvall.Business.PropertyEventData<int> eventData = new Sidvall.Business.PropertyEventData<int>(_ServerItemId);
                OnBeforeServerItemIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<int> eventData = new Sidvall.Business.PropertyChangeEventData<int>(value, _ServerItemId);
                OnBeforeServerItemIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ServerItemId", eventData.Value, true, true, true);
                    OnAfterServerItemIdPropertyChanged();
                }
            }
        }

        #endregion
        #region TypeName

        private string _TypeName = string.Empty;
        partial void OnBeforeTypeNamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeTypeNamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterTypeNamePropertyChanged();
        public string TypeName
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_TypeName);
                OnBeforeTypeNamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _TypeName);
                OnBeforeTypeNamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("TypeName", eventData.Value, true, true, true);
                    OnAfterTypeNamePropertyChanged();
                }
            }
        }

        #endregion
        #region ServerItem

        private ServerItem _ServerItem;
        partial void OnBeforeServerItemPropertyChanged(Sidvall.Business.PropertyChangeEventData<ServerItem> eventData);
        partial void OnAfterServerItemPropertyChanged();
        public ServerItem ServerItem
        {
            get
            {
                return _ServerItem;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<ServerItem> eventData = new Sidvall.Business.PropertyChangeEventData<ServerItem>(value, _ServerItem);
                OnBeforeServerItemPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ServerItem", eventData.Value, true, true, false);
                    OnAfterServerItemPropertyChanged();
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public Plugin Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public Plugin Copy()
        {
            return Copy(null);
        }
        public Plugin Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new Plugin();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.IdentityManager.Data.IPlugin item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.IdentityManager.Data.IPlugin item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(Plugin destination, global::Sidvall.IdentityManager.Data.IPlugin source, Sidvall.Data.CopySettings copySettings);
        private void Import(Plugin destination, global::Sidvall.IdentityManager.Data.IPlugin source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(Plugin destination, Plugin source, Sidvall.Data.CopySettings copySettings);
        private void Import(Plugin destination, Plugin source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.IdentityManager.Data.IPlugin, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.IdentityManager.UI.SystemContextConfig.Current.EntityFacade.PluginManager.Initialize(this, settings);
        }

        #endregion
        
        #region IPlugin Members

        string global::Sidvall.IdentityManager.Data.IPlugin.Context
        {
            get
            {
                return this._Context;
            }
            set
            {
                this._Context = value;
            }
        }
        bool global::Sidvall.IdentityManager.Data.IPlugin.EditIsEnabled
        {
            get
            {
                return this._EditIsEnabled;
            }
            set
            {
                this._EditIsEnabled = value;
            }
        }
        bool global::Sidvall.IdentityManager.Data.IPlugin.FilterIsEnabled
        {
            get
            {
                return this._FilterIsEnabled;
            }
            set
            {
                this._FilterIsEnabled = value;
            }
        }
        System.Nullable<int> global::Sidvall.IdentityManager.Data.IPlugin.PluginId
        {
            get
            {
                return this._PluginId;
            }
            set
            {
                this._PluginId = value;
            }
        }
        int global::Sidvall.IdentityManager.Data.IPlugin.ServerItemId
        {
            get
            {
                return this._ServerItemId;
            }
            set
            {
                this._ServerItemId = value;
            }
        }
        string global::Sidvall.IdentityManager.Data.IPlugin.TypeName
        {
            get
            {
                return this._TypeName;
            }
            set
            {
                this._TypeName = value;
            }
        }
        global::Sidvall.IdentityManager.Data.IServerItem global::Sidvall.IdentityManager.Data.IPlugin.ServerItem
        {
            get
            {
                return this._ServerItem;
            }
            set
            {
                UpdateChildProperty(this._ServerItem, value);
                this._ServerItem = value as ServerItem;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public Plugin()
        {
        }

        public Plugin(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}